package com.poc.etmart.controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.poc.etmart.constants.ETMartConstants;
import com.poc.etmart.daos.ETMartDao;
import com.poc.etmart.entity.PostedItems;
import com.poc.etmart.entity.ReplyPosts;

@SuppressWarnings("serial")
public class ETMartServlet extends HttpServlet implements ETMartConstants{
	
	private static final Logger ETMSLOG = Logger.getLogger(ETMartServlet.class.getName());
	
	public static void main(String args[]){
		ETMSLOG.log(Level.INFO, "inside main testing");
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		String action=req.getParameter(ACTION);
		action=REPLYPOSTS;
		if(ADDALERTS.equalsIgnoreCase(action)){
			this.addAlerts(req);
		}
		if(REPLYPOSTS.equalsIgnoreCase(action)){
			this.replyPosts(req);
		}
	}

	private void replyPosts(HttpServletRequest req) {
		ETMSLOG.log(Level.INFO,"ENTER:inside addalerts");
		ReplyPosts replyPosts = new ReplyPosts();
		replyPosts.setComments(req.getParameter(COMMENTS));
//		replyPosts.setPostedItemsId(Integer.parseInt(req.getParameter(POSTEDITEMSID)));
		replyPosts.setReplierId(req.getParameter(REPLIERID));
		ETMartDao objDao = new ETMartDao();
		objDao.insertIntoReplyPosts(replyPosts);
		ETMSLOG.log(Level.INFO,"EXIT:inside addalerts");
	}

	private void addAlerts(HttpServletRequest req) {
		ETMSLOG.log(Level.INFO,"ENTER:inside addalerts");
		PostedItems postedItems = new PostedItems();
		postedItems.setCategory(req.getParameter(CATEGORY));
		postedItems.setDescription(req.getParameter(DESCRIPTION));
		postedItems.setFeatures(req.getParameter(FEATURES));
		postedItems.setItem(req.getParameter(ITEM));
		postedItems.setPostedBy(req.getParameter(POSTEDBY));
//		postedItems.setPrice(Double.parseDouble(req.getParameter(PRICE)));
//		postedItems.setQuantity(Integer.parseInt(req.getParameter(QUANTITY)));
		ETMartDao objDao = new ETMartDao();
		objDao.insertIntoPostedItems(postedItems);
		ETMSLOG.log(Level.INFO,"EXIT:inside addalerts");
	}
}
